package com.icinfo.cloud.provider.punish.ucase.investigateevidence.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.DocumentCommonSaveResultVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseDocumentRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseTemporaryRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainDetailInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICasePartiesRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseTemporaryRecordService;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.dto.CaseAskNoticeDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.dto.CaseAskNoticePersonaDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.dto.CaseDocumentAuthedEntrustDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.ICaseDocumentAuthedEntrustService;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.IInvestigateService;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.vo.AuthBookVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.vo.CasePartyMsgVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.vo.CasePartyNoticeVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.vo.InvestigateIsEditNextStageVo;
import com.icinfo.framework.common.web.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**调查取证阶段
 * @author lxj
 * @date 2022/2/24
 **/
@RestController
@RequestMapping("/punish/ucase/case/investigate")
@Slf4j
public class InvestigateController {
    @Autowired
    private ICaseDocumentAuthedEntrustService caseDocumentAuthedEntrustService;

    @Autowired
    private ICasePartiesRecordService casePartiesRecordService;

    @Autowired
    private ICaseMainDetailInfoService caseMainDetailInfoService;
    @Resource
    ICaseDocumentRecordService caseDocumentRecordService;
    @Resource
    ICaseTemporaryRecordService caseTemporaryRecordService;
    @Resource
    private IInvestigateService iInvestigateService;
    /**
     * 获取授权委托书页面固定信息
     * @author lxj
     * @date  2022/2/16
     * @param caseId 入参
     * @return 成功、失败
     **/
    @RequestMapping("/getCaseEntrustBookNeedMsg")
    public Result<AuthBookVo> getCaseEntrustBookNeedMsg(@RequestBody String caseId){
        if(StringUtils.isEmpty(caseId)){
            return Result.error("caseId 未传递");
        }
        JSONObject obj = JSON.parseObject(caseId);
        AuthBookVo vo =  caseMainDetailInfoService.getCaseEntrustBookNeedMsg(obj.getString("caseId"), obj.getString("partyId"));
        return Result.success(vo);
    }


    /**
     * 保存授权委托书信息
     * @author lxj
     * @date  2022/2/16
     * @param caseDocumentAuthedEntrustDto 入参
     * @return 成功、失败
     **/
    @RequestMapping("/saveAuthEntrustBook")
    public Result<DocumentCommonSaveResultVo> saveAuthEntrustBook(@RequestBody @Validated CaseDocumentAuthedEntrustDto caseDocumentAuthedEntrustDto) {
        DocumentCommonSaveResultVo resultVo = caseDocumentAuthedEntrustService.saveAuthEntrust(caseDocumentAuthedEntrustDto);
        if (ObjectUtil.isEmpty(resultVo)) {
            return Result.error("保存授权委托书失败");
        }
        return Result.success("success",resultVo);
    }

    /**
     * 案件数据查询--通用
     * @author lxj
     * @date  2022/3/6
     * @param  caseId
     * @return
     **/
    @RequestMapping(value = "/getSaveMsg")
    public Result<JSONObject> getCaseSaveMsg(@RequestBody String caseId) {
        if (StringUtils.isEmpty(caseId)) {
            return Result.error("参数 未传递");
        }
        JSONObject obj = JSON.parseObject(caseId);
        CaseDocumentRecord vo = caseDocumentRecordService.getCaseDocumentRecord(obj.getString("caseId"), obj.getString("documentCatalogCode"), obj.getString("partyId"));
        if (vo == null) {
            CaseTemporaryRecord record = caseTemporaryRecordService.getRecord(obj.getString("caseId"), obj.getString("documentCatalogCode"), obj.getString("partyId"));
            if(record==null){
                return Result.success(Constant.STRING_NOT_FOUND);
            }
            JSONObject jsonObject = JSONObject.parseObject(record.getContent());
            return Result.success(jsonObject);
        }
        JSONObject jsonObject = JSONObject.parseObject(vo.getDocumentContent());
        return Result.success(jsonObject);
    }

    /**
     * 获取授权委托人+当事人信息
     * @author lxj
     * @date  2022/3/2
     * @param  caseId
     * @return
     **/
    @RequestMapping("/getAuthorizedPersonAndPartyList")
    public Result getAuthorizedPersonAndPartyList(@RequestBody String caseId) {
        if (StringUtils.isEmpty(caseId)) {
            return Result.error("caseId 未传递");
        }
        JSONObject obj = JSON.parseObject(caseId);
        List<CasePartyMsgVo> list = casePartiesRecordService.getAuthorizedPersonAndPartyList(obj.getString("caseId"), obj.getString("partyId"));
        return Result.success(list);
    }

    /**
     * 获取案件基础信息（如：询问通知书）
     * @author lxj
     * @date  2022/3/2
     * @param  caseId
     * @return
     **/
    @RequestMapping("/getAskNoticeMsg")
    public Result<CasePartyNoticeVo> getAskNoticeMsg(@RequestBody String caseId){
        try {
            if(StringUtils.isEmpty(caseId)){
                return Result.error("caseId 未传递");
            }
            JSONObject obj = JSON.parseObject(caseId);
            CasePartyNoticeVo vo = caseMainDetailInfoService.getNoticeMsg(obj.getString("caseId"), obj.getString("partyId"));
            return Result.success(vo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 保存询问通知书
     * @author lxj
     * @date  2022/3/2
     * @param caseAskNoticeDto
     * @return
     **/
    @RequestMapping("/saveAskNoticeBook")
    public Result<Map<String,Object>> saveAskNoticeBook(@RequestBody @Validated CaseAskNoticeDto caseAskNoticeDto) {
        Map<String, Object> resultMap = caseMainDetailInfoService.saveAskNoticeBook(caseAskNoticeDto);
        return Result.success("success",resultMap);
    }


    /**
     * 描述: 保存人设条线的询问通知书
     * @author zhanghongqiang
     * @Date 2022/10/26
     * @param caseAskNoticePersonaDto
     * @return com.icinfo.framework.common.web.Result<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @RequestMapping("/saveAskNoticeBookPersona")
    public Result<Map<String,Object>> saveAskNoticeBookPersona(@RequestBody @Validated CaseAskNoticePersonaDto caseAskNoticePersonaDto) {
        Map<String, Object> resultMap = caseMainDetailInfoService.saveAskNoticeBookPersona(caseAskNoticePersonaDto);
        return Result.success("success",resultMap);
    }

    /**
     * 【V1.11.4】描述：校验是调查取证下一环节是否可以操作
     *
     * @param caseId 用例id
     * @return {@link Result<Boolean> }
     * @author shishengyao
     * @date 2023/03/24
     */
    @GetMapping("/isCanEditNextStage")
    @ApiOperation(value = "【V1.11.2】描述：是否存在未完成的调整当事人接口记录", httpMethod = "GET")
    public Result<InvestigateIsEditNextStageVo> isCanEditNextStage(@RequestParam("caseId") String caseId){
        return Result.success(iInvestigateService.isEditNextStage(caseId));
    }
}
